﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class MP_PositionCommandLines : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        Main main = new Main();
        private IPositionBLL posBLL = new PositionBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindTreeCommandLine(RadTreeView1);
                BindTreeWorkingCommandLine(RadTreeView2);
            }
        }

        private void BindTreeCommandLine(RadTreeView treeView)
        {
            IList<PositionInfo> InfoList = posBLL.LoadPositionTreeView(1);

            treeView.DataSource = InfoList;
            treeView.DataBind();
            treeView.ExpandAllNodes();
        }

        private void BindTreeWorkingCommandLine(RadTreeView treeView)
        {
            IList<PositionInfo> InfoList = posBLL.LoadWorkingTreeView(1);

            treeView.DataSource = InfoList;
            treeView.DataBind();
            treeView.ExpandAllNodes();
        }

        protected void RadTreeView1_NodeDataBound(object sender, RadTreeNodeEventArgs e)
        {
            //e.Node.ToolTip = (e.Node.DataItem as DataRowView)["DETAIL"].ToString();
            //e.Node.ImageUrl = "../Image/Buttons/Button_Blue.jpg";
        }
        
        protected void RadTreeView1_NodeDrop(object sender, RadTreeNodeDragDropEventArgs e)
        {
            PositionInfo Info = new PositionInfo();

            foreach (RadTreeNode node in e.DraggedNodes)
            {
                RadTreeNode destNode = e.DestDragNode;

                string nodeUpper = destNode.Value;//nodeValueHiddenField.Value.Trim();
                nodeUpper = nodeUpper == "" ? "0" : nodeUpper;

                int Seq = node.Value.Length > 0 ? Int32.Parse(node.Value) : 0;
                string Upper = nodeUpper.Length > 0 ? nodeUpper : "";

                posBLL.UpdateUpper(Seq, Upper, ssMng.EmpSeqI);
            }

            BindTreeCommandLine(RadTreeView1);
        }

        protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            BindTreeCommandLine(RadTreeView1);
        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {
            RadTreeNodeCollection nodes = this.RadTreeView1.Nodes;
            RadTreeNodeCollection nodes2 = this.RadTreeView2.Nodes;
            string strSearch = txtSearch.Text.TrimEnd();

            // ค้นหาในกฏกระทรวง
            foreach (RadTreeNode n in nodes)
            {
                n.BackColor = RadTreeView1.BackColor;
                main.ClearRecursive(n, RadTreeView1.BackColor);
            }

            foreach (RadTreeNode n in nodes)
            {
                if (strSearch.Length > 0)
                {
                    if (n.Text.IndexOf(strSearch) >= 0)
                    {
                        n.BackColor = System.Drawing.Color.Pink;
                        n.ExpandParentNodes();
                    }
                    main.FindRecursive(n, strSearch);
                }
            }

            // ค้นหาในโครงสร้างตามมอบหมายงาน
            foreach (RadTreeNode n in nodes2)
            {
                n.BackColor = RadTreeView2.BackColor;
                main.ClearRecursive(n, RadTreeView2.BackColor);
            }

            foreach (RadTreeNode n in nodes2)
            {
                if (strSearch.Length > 0)
                {
                    if (n.Text.IndexOf(strSearch) >= 0)
                    {
                        n.BackColor = System.Drawing.Color.Pink;
                        n.ExpandParentNodes();
                    }
                    main.FindRecursive(n, strSearch);
                }
            }
        }

        protected void cmdExpand_Click(object sender, ImageClickEventArgs e)
        {
            RadTreeView1.ExpandAllNodes();
            RadTreeView2.ExpandAllNodes();
        }

        protected void cmdCollapse_Click(object sender, ImageClickEventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
            RadTreeView2.CollapseAllNodes();
        }

        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {

        }

        
        protected void RadTreeView2_NodeDataBound(object sender, RadTreeNodeEventArgs e)
        {
            //if (e.Node.ParentNode.Value == "0")
            //{
            //    e.Node.Font.Italic = true;
            //    e.Node.ForeColor = System.Drawing.Color.Gray;
            //}
            //else 
            //{
            //    e.Node.ForeColor = System.Drawing.Color.BlueViolet;
            //}

            //e.Node.ToolTip = (e.Node.DataItem as DataRowView)["JobDescription"].ToString();

            // เปลี่ยนสีของ icon
            //if (e.Node.Level == 0)
            //{
            //    e.Node.ImageUrl = "../Images/Buttons/box_blue.gif";
            //}
            //else if (e.Node.Level == 1)
            //{
            //    e.Node.ImageUrl = "../Images/Buttons/box_green.gif";
            //}
            //else if (e.Node.Level == 2)
            //{
            //    e.Node.ImageUrl = "../Images/Buttons/box_orange.gif";
            //}
            //else if (e.Node.Level == 3)
            //{
            //    e.Node.ImageUrl = "../Images/Buttons/bgreen.gif";
            //}
            //else 
            //{
            //    e.Node.ImageUrl = "../Images/Buttons/box_orange.gif";
            //}
        }

        protected void RadTreeView2_NodeDrop(object sender, RadTreeNodeDragDropEventArgs e)
        {
            PositionInfo Info = new PositionInfo();

            foreach (RadTreeNode node in e.DraggedNodes)
            {
                RadTreeNode destNode = e.DestDragNode;

                string nodeUpper = destNode.Value;//nodeValueHiddenField.Value.Trim();
                nodeUpper = nodeUpper == "" ? "0" : nodeUpper;

                int Seq = node.Value.Length > 0 ? Int32.Parse(node.Value) : 0;
                string Upper = nodeUpper.Length > 0 ? nodeUpper : "";

                posBLL.UpdateWorkingUpper(Seq, Upper, ssMng.EmpSeqI);
            }

            BindTreeWorkingCommandLine(RadTreeView2);

        }
    }
}
